from django.http import HttpResponse
from django.shortcuts import render, redirect
from .. import models
from ..forms import UpModelForm


def city_list(requeset):
    queryset = models.City.objects.all()
    return render(requeset, "app_02/city_list.html", {"queryset": queryset})


def city_add(request):
    """ 上传modelform """
    title = "新建城市"
    if request.method == "GET":
        form = UpModelForm()
        return render(request, "app_02/upload_form.html", {"form": form, "title": title})

    form = UpModelForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        # 对于文件：自动保存
        form.save()
        return redirect("/app_02/city/list/")

    return render(request, "app_02/upload_form.html", {"form": form, "title": title})